package com.tanhua.dubbo.server.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @Author Au
 * @DATE 2020/11/24 18:28
 **/
/*
 *   原理:使用redis自增长类型,实现自增长的id
 * */
@Service
public class IdService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 生成自增长的id
     *
     * @return
     */
    public Long createId(String type,String objectId) {

        type = StringUtils.upperCase(type);

        String hashKey = "TANHUA_HASH_ID_" + type;
        //如果Objectid已经存在的话,就返回对应的id
        if (redisTemplate.opsForHash().hasKey(hashKey,objectId)) {
            return Long.valueOf(redisTemplate.opsForHash().get(hashKey,objectId).toString());
        }

        String key = "TANHUA_ID_" + type;
        Long id = redisTemplate.opsForValue().increment(key);

        redisTemplate.opsForHash().put(hashKey,objectId,id.toString());

        return id;

    }
}
